package cn.bdqn.controller;

import java.io.File;

import javax.mail.search.IntegerComparisonTerm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.bdqn.entity.Repair;
import cn.bdqn.entity.UsersAndRepair;
import cn.bdqn.service.RepairService;
import cn.bdqn.util.Page;

@Controller
@RequestMapping("repair")
public class RepairController {
	@Autowired
	private RepairService service;
	
	@RequestMapping("saveRepair")
	public String saveRepair(Integer _userid,HttpSession session, HttpServletRequest request,Repair repair,Model model,@RequestParam(value="a_idPicPath",required=false) MultipartFile attach) {
		String postimg=null;
		repair.set_userid(_userid);
		//判断文件是否为空
		if(!attach.isEmpty()) {
			//定义文件上传路径
			String path=request.getSession().getServletContext().getRealPath("statics"+File.separator+"images");
			String oldFileName=attach.getOriginalFilename();
			String prefix=FilenameUtils.getExtension(oldFileName);
			int filesize=5000000;
			if(attach.getSize()>filesize) {
				request.setAttribute("message", "* 文件大小不得超过500k");
				return "main";
			}else if(prefix.equalsIgnoreCase("jpg")||prefix.equalsIgnoreCase("jpeg")||prefix.equalsIgnoreCase("png")||prefix.equalsIgnoreCase("pneg")) {
				//获取当前系统时间和随机数+"_Personal.jpg"
				String fileName=System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"_Personal.jpg";
				File targetFile=new File(path, fileName);
				if(!targetFile.exists()) {
					targetFile.mkdirs();
				}
				try {
					attach.transferTo(targetFile);
				} catch (Exception e) {
					e.printStackTrace();
					request.setAttribute("message", "* 上传失败");
					return "main";
				}
				postimg="\\dormitory\\statics\\images"+File.separator+fileName;
			}else {
				request.setAttribute("message", "* 上传图片格式必须是jpg、jpeg、png、pneg");
				return "main";
			}
			repair.setContextimg(postimg);
		}
		Integer count=service.saveRepair(repair);
		System.out.println("sdsds");
		return "forward:/users/page";
	}
	@RequestMapping("updateRepair")
	public String updateRepair(Repair repair) {
		if(repair.getStatus()==1) {
			repair.setStatus(2);
			Integer num=service.updateRepair(repair);
		}else if(repair.getStatus()==2) {
			repair.setStatus(1);
			Integer num2=service.updateRepair(repair);
		}
		return "forward:/users/page";
	}
	//处理报修
	@RequestMapping("tochurepair")
	public String tochurepair(String context,Integer pageNo,Integer pageSize,Model model) {
		if(pageNo==null) {
			pageNo=1;
		}
		if(pageSize==null) {
			pageSize=3;
		}
		if(context==null) {
			context="";
		}
		Page<UsersAndRepair> page=new Page<>();
		page.setPageSize(pageSize);
		page.setPageNo(pageNo);
		service.queryAllRepairByguan(context, page);
		model.addAttribute("page", page);
		model.addAttribute("context", context);
		return "chuRepair";
	}
	//未处理报修
	@RequestMapping("toweirepair")
	public String toweirepair() {
		return "weiRepair";
	}
	//修改处理未处理
	@RequestMapping("updatechustatus")
	public String updatechustatus(Repair repair,Integer pageNo) {
		if(repair.getChustatus()==1) {
			repair.setChustatus(2);
		}else if(repair.getChustatus()==2) {
			repair.setChustatus(1);
		}
		Integer num=service.updateRepairChuStatus(repair);
		return "forward:/repair/tochurepair?pageNo="+pageNo;
	}
	//查看详情
	@RequestMapping("detailrepair")
	public String detailRepair(Integer repairid,Model model) {
		UsersAndRepair repair=service.queryRepairByrepirid(repairid);
		model.addAttribute("repair", repair);
		return "toguandetailrepair";
	}
}
